from PyQt5.QtWidgets import QApplication, QMainWindow, QWidget, QVBoxLayout, QHBoxLayout, QPushButton, QDockWidget, QGraphicsPolygonItem,QGraphicsLineItem,QGraphicsView, QGraphicsScene, QGraphicsRectItem, QComboBox,QGraphicsEllipseItem
from PyQt5.QtGui import QPainter, QPen, QBrush, QColor, QFont, QPolygonF
from PyQt5.QtCore import Qt, QRectF, QPointF, QLineF

class RulerWidget(QWidget):
    def __init__(self, orientation, parent=None):
        super().__init__(parent)
        self.orientation = orientation
        self.scale = 1.0
        self.setMinimumSize(50, 50)
        self.setAutoFillBackground(True)

    def paintEvent(self, event):
        painter = QPainter(self)
        self.drawRuler(painter)

    def drawRuler(self, painter):
        pen = QPen(Qt.black, 1)
        painter.setPen(pen)
        pen_major = QPen(Qt.black, 2)
        pen_minor = QPen(Qt.gray, 1)
        font = QFont()
        font.setPixelSize(10)
        painter.setFont(font)

        if self.orientation == Qt.Horizontal:
            ruler_length = self.width()
            for i in range(-280, ruler_length, int(2 * self.scale)):
                pos = i + 280
                if i % (20 * self.scale) == 0:
                    painter.setPen(pen_major)
                    painter.drawLine(pos+10, 0, pos+10, 15)
                    painter.drawText(pos + 2, 10, str(int(i)))
                elif i % (4 * self.scale) == 0:
                    painter.setPen(pen_minor)
                    painter.drawLine(pos+10, 0, pos+10, 10)
        else:
            ruler_length = self.height()
            for i in range(-280, ruler_length, int(2 * self.scale)):
                pos = i + 280
                if pos % int(20 * self.scale) == 0:
                    painter.setPen(pen_major)
                    painter.drawLine(0, pos, 15, pos)
                    painter.drawText(2, pos + 10, str(int(i)))
                elif pos % int(4 * self.scale) == 0:
                    painter.setPen(pen_minor)
                    painter.drawLine(0, pos, 10, pos)

    def setScale(self, scale):
        self.scale = scale
        self.update()